<template>
    <nav class="nav flex aic jc-sb">
        <div class="left flex aic ml-10">
            <p @click="more" class="cp"
                :class="{'iconfont icon-gengduo':!store.state ,'iconfont icon-gengduo1':store.state}"></p>
            <el-input v-model="input2" class="w-50 ml-20" placeholder="Search" :prefix-icon="Search" />
        </div>
        <div class="right flex aic">
            <p class="item">
                <i class="iconfont icon-mn_pengyou"></i>
                <span>1</span>
            </p>
            <p class="item">
                <i class="iconfont icon-xitongxiaoxi"></i>
                <span>2</span>
            </p>
            <p class="item">
                <i class="iconfont icon-yuyanqiehuan"></i>
            </p>
            <div v-if="store.token" class="item flex fdc login">
                <img src="../../assets/img/touxiang.jpg" alt="">
                <div class="box-card bs pt-5 pb-5">
                    <p>设置</p>
                    <p @click="secede">退出登录</p>
                </div>
            </div>
            <p v-else class="cp" @click="dialog = true">登 录</p>
        </div>
    </nav>

    <!-- 登录弹窗 -->
    <div v-if="dialog" class="masked">
        <div class="login flex fdc aic fff">
            <h3>Login</h3>
            <input class="inp mt-30" v-model="usemame" type="text" placeholder="usemame">
            <input class="inp mt-20" v-model="password" type="password" placeholder="password">
            <div class="mt-30 flex jc-sa">
                <el-button @click="login" type="primary">Login</el-button>
                <el-button @click="dialog = false">Cancel</el-button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useDemoStore } from '../../pinia/index'
import * as $axios from '../../api/index'

const store = useDemoStore()
const input2 = ref('')

const more = () => {
    store.changeState(!store.state);
}
const dialog = ref(false)

const usemame = ref('')
const password = ref('')

// 登录
const login = () => {
    // console.log(usemame.value)
    const data = {
        username: usemame.value,
        pwd: password.value
    }
    $axios.$login(data).then((res) => {
        console.log(res.data)
        if (res.Status == 0) {
            ElMessage({
                message: 'Login Success',
                type: 'success',
            })
            dialog.value = false
            store.changeToken(res.data.token);
            store.changeName(res.data.user_group[0].UserGroupName);
            store.changeType(res.data.type)
            // 订单权限暂时使用
            emits('confirmPermission')
        }
    })
}

// 退出登录
const secede = () => {
    store.changeToken('')
}
const emits = defineEmits(['confirmPermission'])
</script>

<style lang="less" scoped>
@import '../../assets/iconfont.css';

.nav {
    width: 100%;
    height: 6vh;
    background-color: #f1f1f1;

    .iconfont{
        font-size: 25px;
    }

    .el-input {
        width: 100%;
    }

    .right {
        width: 20%;
        height: 100%;
        justify-content: space-around;

        .login:hover .box-card {
            display: block;
        }

        .item {
            display: flex;
            align-items: center;
            position: relative;

            .box-card {
                display: none;
                position: absolute;
                width: 70px;
                top: 38px;
                z-index: 1;
                background-color: #fff;
                border-radius: 3px;
                // padding: 4px;
                text-align: center;
                border: 1px solid #f3f3f3;
                font-size: 13px;
                line-height: 1.8;

                p:hover {
                    background-color: rgb(127, 214, 255);
                    color: #fff;
                    cursor: pointer;
                }
            }

            img {
                width: 35px;
                height: 35px;
                border-radius: 35px;
                z-index: 2;
            }

            span {
                width: 20px;
                height: 20px;
                border: 1px solid;
                position: absolute;
                top: -5px;
                right: -10px;
                border-radius: 50%;
                background-color: #F56C6C;
                border: none;
                text-align: center;
                color: #fff;
            }
        }

    }

}


.masked {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0 auto;
    z-index: 9;
    background-color: #000000a8;

    .login {
        margin: 0 auto;
        padding: 10px;
        margin-top: 13%;
        width: 22%;
        height: 240px;
        background-image: url('../../assets/img/login.jpg');
        background-size: contain;
        border-radius: 7px;

        .inp {
            border: none;
            background-color: #ffffff00;
            outline: none;
            border-bottom: 1px solid #fff;
            width: 80%;
            font-size: 16px;
            line-height: 2;
            color: rgb(255, 255, 255);
            padding: 0 5px;

        }
    }
}
</style>